Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize drag and click of Pointer #2042

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

cptbtptpbcptdtptp
Copy link
Collaborator

@cptbtptpbcptdtptp cptbtptpbcptdtptp commented Mar 18, 2024

  • Script new callback about drag and drop
  • Pointer new API
  • Update pointer unit test

Summary by CodeRabbit

  • New Features

    • Enhanced pointer interaction functionality with new handling for drag events (onPointerBeginDrag, onPointerEndDrag, and onPointerDrop).
    • Introduced a new property to capture raycasting results and restructured entity tracking for improved interaction.
  • Bug Fixes

    • Improved accuracy and reliability of pointer event handling.
  • Tests

    • Added comprehensive tests for new pointer event features and raycasting hit results to ensure functionality and performance.
  • Refactor

    • Streamlined internal pointer event logic and entity tracking for better maintainability and performance.

@cptbtptpbcptdtptp cptbtptpbcptdtptp added bug Something isn't working Input Input related functions labels Mar 18, 2024
@cptbtptpbcptdtptp cptbtptpbcptdtptp added this to the 1.2 milestone Mar 18, 2024
@cptbtptpbcptdtptp cptbtptpbcptdtptp self-assigned this Mar 18, 2024
Copy link

codecov bot commented Mar 18, 2024

Codecov Report

Attention: Patch coverage is 96.36364% with 2 lines in your changes missing coverage. Please review.

Project coverage is 66.07%. Comparing base (025033e) to head (5248421).
Report is 221 commits behind head on main.

Files Patch % Lines
packages/core/src/input/pointer/Pointer.ts 96.96% 1 Missing ⚠️
packages/core/src/input/pointer/PointerManager.ts 94.73% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2042      +/-   ##
==========================================
- Coverage   67.46%   66.07%   -1.39%     
==========================================
  Files         471      462       -9     
  Lines       23809    23131     -678     
  Branches     3402     3413      +11     
==========================================
- Hits        16062    15284     -778     
- Misses       6556     6669     +113     
+ Partials     1191     1178      -13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@crazylxr crazylxr modified the milestones: 1.2, 1.3 May 21, 2024
@cptbtptpbcptdtptp cptbtptpbcptdtptp changed the title Fix the logic problem of onPointerUp and onPointerClick Optimize drag and click of Pointer Jun 4, 2024
* This function will be called when the pointer is pressed on the collider.
* @param pointer
*/
onPointerStartDrag(pointer: Pointer): void {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use onPointerBeginDrag name, like onBeginRender ane onEndRender

@GuoLei1990 GuoLei1990 added the enhancement New feature or request label Jul 16, 2024
Copy link

coderabbitai bot commented Jul 16, 2024

Walkthrough

The updates enhance pointer event handling in the Script class and improve the Pointer functionality with new properties and methods. Key features include managing drag-related events and tracking entities during pointer interactions. Additionally, unit tests in InputManager.test.ts are revamped to ensure thorough testing of these pointer interactions, ensuring robust performance and reliability.

Changes

File Change Summary
packages/core/src/Script.ts Added onPointerBeginDrag, onPointerEndDrag, and onPointerDrop methods to the Script class.
packages/core/src/input/pointer/Pointer.ts Introduced new properties and methods for pointer interactions, including event handling and entity tracking.
tests/src/core/input/InputManager.test.ts Adjusted entity positions, camera setup, and added tests for new pointer events and hit results.

Poem

A rabbit hopped with code so bright,
It brought new features into light.
With pointers dragging here and there,
Entities now handled with care.
In tests we trust, to guide our way,
For better code, hip-hip-hooray! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1dda49d and 5454fd6.

Files selected for processing (4)
  • packages/core/src/Script.ts (1 hunks)
  • packages/core/src/input/pointer/Pointer.ts (5 hunks)
  • packages/core/src/input/pointer/PointerManager.ts (7 hunks)
  • tests/src/core/input/InputManager.test.ts (5 hunks)
Additional comments not posted (27)
packages/core/src/input/pointer/Pointer.ts (13)

5-5: Import of HitResult

The import statement for HitResult is necessary for the new hitResult property. This change is appropriate and follows good practice for managing dependencies.


29-29: Initialization of hitResult

The hitResult property is initialized correctly. This property is essential for storing the results of raycasting operations, which are crucial for pointer interactions in 3D environments.


33-33: Initialization of _eventsMap

The _eventsMap property is initialized to PointerEventType.None, which is a safe default ensuring that no pointer events are mistakenly flagged at initialization.


45-47: Private entity tracking properties

The introduction of _pressedEntity, _enteredEntity, and _draggedEntity is crucial for managing the state of entities during different pointer interactions. This change enhances the tracking capabilities of the Pointer class.


52-54: Getter methods for entity properties

The getter methods for pressedEntity, enteredEntity, and draggedEntity are implemented correctly. These methods provide safe, read-only access to the internal state, which is a good practice in encapsulation.

Also applies to: 59-61, 66-68


73-75: Constructor implementation

The constructor correctly initializes the id property of the Pointer instance. This is essential for uniquely identifying each pointer, especially in multi-touch scenarios.


Line range hint 81-102: Method to handle pointer exit and enter events

This method efficiently manages transitions between different entities during pointer interactions. The use of _scripts to trigger specific script actions is a robust design choice, facilitating complex interactions within the game or application environment.


109-122: Method to handle pointer down and start drag events

This method consolidates the actions related to pointer down and drag start, reducing redundancy and enhancing the clarity of event handling. The simultaneous update of _pressedEntity and _draggedEntity is a sensible approach, reflecting the start of a drag action.


127-140: Method to handle pointer up and click events

The conditional check for sameTarget before firing the onPointerClick event is a crucial detail that ensures the click event is only triggered when the pointer up event occurs on the same entity as the pointer down event. This prevents false positives in click detection.


147-148: Method to fire pointer drag events

This method correctly triggers the onPointerDrag event for the _draggedEntity. It's well-implemented to support continuous drag interactions, crucial for user interfaces and interactive applications.


162-174: Method to handle pointer end drag events

The separation of concerns is well maintained here, with clear handling of the end drag event. The conditional check for receivingEntity to trigger onPointerDrop is a thoughtful addition, allowing for more complex drag-and-drop interactions.


181-183: Dispose method implementation

The _dispose method correctly nullifies all relevant properties, ensuring that the pointer instance is cleaned up properly. This is crucial for managing memory and preventing leaks in applications with complex interactions.


187-194: PointerEventType enum

The introduction of the PointerEventType enum is a significant enhancement for managing different types of pointer events. This structured approach facilitates clearer and more maintainable event handling code.

packages/core/src/Script.ts (2)

166-167: Implementation of onPointerEndDrag

The method is implemented correctly, providing a clear and specific handler for the end of a drag event. This addition enhances the script's capabilities to react to complex pointer interactions.


172-173: Implementation of onPointerDrop

The onPointerDrop method is a new addition that handles the scenario where a pointer is lifted from a collider during a drag operation. This method is crucial for implementing drag-and-drop functionality.

packages/core/src/input/pointer/PointerManager.ts (7)

12-12: Import of Pointer and PointerEventType

The import of Pointer and PointerEventType is necessary for the updated functionality in this file. This change is appropriate and integrates well with the rest of the codebase.


76-76: Pointer disposal in the update loop

The addition of the _dispose call within the cleanup loop is a critical improvement. It ensures that pointer instances are properly cleaned up, preventing memory leaks and ensuring that the system's state is correctly maintained.


116-116: Reset of _eventsMap in the update loop

Resetting _eventsMap to PointerEventType.None at the start of each update cycle is a good practice. It ensures that event states do not persist incorrectly across frames.


130-135: Pointer raycasting method call

The modification to include hitResult in the _pointerRayCast method call is a necessary update to support the new functionality in the Pointer class. This change allows for more accurate and detailed handling of pointer interactions.


139-142: Handling of pointer move events

The handling of pointer move events with the new _firePointerDrag method call is an appropriate update. It ensures that drag events are processed correctly, enhancing the responsiveness and accuracy of the interface.


148-153: Enhanced event handling for pointer down and up

The updates to handle pointer down and up events more comprehensively, including the initiation and termination of drag operations, are well-implemented. These changes make the event handling more robust and intuitive.


159-159: Handling of pointer leave and cancel events

The addition of _firePointerEndDrag in the context of pointer leave and cancel events is a crucial update. It ensures that drag operations are correctly terminated even when unexpected events like pointer leave occur.

tests/src/core/input/InputManager.test.ts (5)

84-86: Setup of camera and box entities in tests

The adjustments to the camera and box entities, including their positions and sizes, are appropriate for testing the new pointer functionality. These changes ensure that the tests are relevant and effective for the new features.

Also applies to: 89-89, 92-92


108-110: Implementation of pointer event handlers in TestScript

The new methods onPointerUp, onPointerStartDrag, onPointerEndDrag, and onPointerDrop in the TestScript class are correctly implemented. These methods enhance the test coverage for the new pointer event functionality.

Also applies to: 116-118, 124-126, 128-129


138-138: Spying on methods and dispatching events in tests

The use of chai.spy on new methods and the dispatching of pointer events to test these methods are correctly done. These tests effectively verify the functionality of the new event handlers.

Also applies to: 142-154, 162-162


182-211: New tests for onPointerClick

The new tests for the onPointerClick event handler are comprehensive and well-structured. They effectively test various scenarios to ensure that the handler behaves as expected under different conditions.


235-261: Hit result tests

The tests for hit results are crucial for verifying the accuracy of pointer interactions with 3D objects. These tests are well-implemented, covering various scenarios to ensure the functionality is robust and reliable.

* This function will be called when the pointer is pressed on the collider.
* @param pointer
*/
onPointerStartDrag(pointer: Pointer): void {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method naming consistency

The previous comment suggests renaming onPointerStartDrag to onPointerBeginDrag for consistency with other method names like onBeginRender. This is a valid suggestion to maintain a consistent naming convention throughout the API.

- onPointerStartDrag(pointer: Pointer): void {}
+ onPointerBeginDrag(pointer: Pointer): void {}
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
onPointerStartDrag(pointer: Pointer): void {}
onPointerBeginDrag(pointer: Pointer): void {}

* This function will be called when the pointer is lifted on the collider.
* @param pointer - The pointer that triggered
*/
onPointerDrop(pointer: Pointer): void {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure the comments is right and clearly

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5454fd6 and 1068d34.

Files selected for processing (3)
  • packages/core/src/Script.ts (1 hunks)
  • packages/core/src/input/pointer/Pointer.ts (5 hunks)
  • tests/src/core/input/InputManager.test.ts (5 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/core/src/Script.ts
  • tests/src/core/input/InputManager.test.ts
Additional comments not posted (11)
packages/core/src/input/pointer/Pointer.ts (11)

28-29: LGTM! Addition of hitResult property.

The hitResult property captures raycasting results for the current frame, enhancing pointer interaction tracking.


45-47: LGTM! Renaming and addition of properties.

The renaming of _currentPressedEntity to _pressedEntity and _currentEnteredEntity to _enteredEntity improves readability. The addition of _draggedEntity is logical for tracking drag events.


52-68: LGTM! Addition of getter methods.

The addition of getter methods for pressedEntity, enteredEntity, and draggedEntity provides convenient access to these properties.


73-75: LGTM! Addition of constructor.

The constructor initializes the id property, which is necessary for the Pointer class.


Line range hint 81-102:
LGTM! Update to _firePointerExitAndEnter.

The update to use _enteredEntity improves consistency with the renamed property.


109-122: LGTM! Renaming and update to _firePointerDownAndStartDrag.

The renaming and update to initialize _draggedEntity are logical for handling drag events.


127-140: LGTM! Addition of _firePointerUpAndClick.

The addition of _firePointerUpAndClick is logical for managing pointer up events and clicks.


Line range hint 147-152:
LGTM! Update to _firePointerDrag.

The update to use _draggedEntity is logical for handling drag events.


162-174: LGTM! Addition of _firePointerEndDrag.

The addition of _firePointerEndDrag is logical for handling the end of dragging.


181-183: LGTM! Addition of _dispose.

The addition of _dispose is logical for resetting the state of the pointer.


187-194: LGTM! Addition of PointerEventType enum.

The addition of PointerEventType enum is logical for categorizing pointer events.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1068d34 and 5248421.

Files selected for processing (1)
  • packages/core/src/Script.ts (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/Script.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request Input Input related functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants